package com.t4.process.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t4.common.result.Result;
import com.t4.model.process.Process;
import com.t4.model.process.ProcessTemplate;
import com.t4.model.process.ProcessType;
import com.t4.process.service.ProcessRecordService;
import com.t4.process.service.ProcessService;
import com.t4.process.service.ProcessTemplateService;
import com.t4.process.service.ProcessTypeService;
import com.t4.vo.process.ApprovalVo;
import com.t4.vo.process.ProcessFormVo;
import com.t4.vo.process.ProcessQueryVo;
import com.t4.vo.process.ProcessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "审批流管理")
@RestController
@RequestMapping("/admin/process")
@CrossOrigin // 解决跨域
public class ProcessController {

    @Resource
    private ProcessService processService;

    @Resource
    private ProcessTypeService processTypeService;

    @Resource
    private ProcessTemplateService processTemplateService;




    @ApiOperation(value = "获取分页列表")
    @GetMapping("{current}/{size}")
    public Result pageQuery(@PathVariable Long current,
                            @PathVariable Long size,
                            ProcessQueryVo processQueryVo){
        Page<ProcessVo> pageParam = new Page<>(current, size);
        IPage<ProcessVo> pageModel = processService.selectPage(pageParam, processQueryVo);
        return Result.success(pageModel);
    }


    @ApiOperation(value = "获取全部审批分类及对应模板")
    @GetMapping("findProcessType")
    public Result findProcessType(){
        List<ProcessType> processTypeList = processTypeService.findProcessType();
        return Result.success(processTypeList);
    }

    @ApiOperation(value = "获取审批模板")
    @GetMapping("getProcessTemplate/{processTemplateId}")
    public Result getProcessTemplate(@PathVariable Long processTemplateId){
        ProcessTemplate processTemplate = processTemplateService.getById(processTemplateId);
        return Result.success(processTemplate);
    }

    @ApiOperation(value = "启动流程")
    @PostMapping("startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo){
        // 启动流程实例
        processService.startUp(processFormVo);
        return Result.success();
    }


    @ApiOperation(value = "查询待处理任务")
    @GetMapping("findPending/{current}/{size}")
    public Result findPending(@PathVariable Long current,
                              @PathVariable Long size){
        Page<Process> pageParam = new Page<>(current, size);
        IPage<ProcessVo> processVoIPage = processService.findPending(pageParam);
        return Result.success(processVoIPage);
    }

    @ApiOperation(value = "获取审批详情")
    @GetMapping("show/{id}")
    public Result show(@PathVariable Long id){
        Map<String, Object> map = processService.show(id);
        return Result.success(map);
    }

    @ApiOperation(value = "审批")
    @PostMapping("approve")
    public Result approve(@RequestBody ApprovalVo approvalVo){
        processService.approve(approvalVo);
        return Result.success();
    }

    @ApiOperation(value = "查询已处理")
    @GetMapping("/findProcessed/{current}/{size}")
    public Result findProcessed(@PathVariable Long current,
                                @PathVariable Long size){
        Page<Process> pageParam = new Page<>(current, size);
        IPage<ProcessVo> page = processService.findProcessed(pageParam);
        return Result.success(page);
    }

    @ApiOperation(value = "查询已发起")
    @GetMapping("/findStarted/{current}/{size}")
    public Result findStarted(@PathVariable Long current,
                                @PathVariable Long size){
        Page<ProcessVo> pageParam = new Page<>(current, size);
        IPage<ProcessVo> page = processService.findStarted(pageParam);
        return Result.success(page);
    }
}
